﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Plumbing
{
	public partial class FrmCalFireHydant : System.Windows.Forms.Form
	{
		public FrmCalFireHydant(IList<Element> list, Document doc)
		{
			this.InitializeComponent();
			base.CenterToScreen();
			this.listElement = list;
			this.doc = doc;
		}

		private void FrmCalFireHydant_Load(object sender, EventArgs e)
		{
			this.comboBoxNozzleDiameter.Items.Add("13");
			this.comboBoxNozzleDiameter.Items.Add("16");
			this.comboBoxNozzleDiameter.Items.Add("19");
			this.comboBoxFireDiameter.Items.Add("50");
			this.comboBoxFireDiameter.Items.Add("65");
			this.comboBoxFireDiameter.Items.Add("80");
			this.comboBoxFireMaterial.Items.Add("麻织");
			this.comboBoxFireMaterial.Items.Add("帆布");
			this.comboBoxFireMaterial.Items.Add("衬胶");
			this.comboBoxFireLength.Items.Add("20");
			this.comboBoxFireLength.Items.Add("25");
			this.comboBoxNozzleAngle.Items.Add("45");
			this.comboBoxNozzleAngle.Items.Add("60");
			this.buttonTXT.Enabled = false;
			this.button1.Enabled = false;
			foreach (Element element in this.listElement)
			{
				this.listBox1.Items.Add((element as FamilyInstance).Symbol.Family.Name + " " + element.Name);
			}
			for (int i = 0; i < this.listBox1.Items.Count; i++)
			{
				for (int j = i + 1; j < this.listBox1.Items.Count; j++)
				{
					if (this.listBox1.Items[i].ToString() == this.listBox1.Items[j].ToString())
					{
						this.listBox1.Items.RemoveAt(j);
						j--;
					}
				}
			}
			this.xmlHelper.creat("configuration");
			this.listMoRen = this.xmlHelper.readXML("configuration");
			List<string> list = new List<string>();
			foreach (FireHydantStruct fireHydantStruct in this.listMoRen)
			{
				list.Add(fireHydantStruct.FireName);
			}
			for (int k = 0; k < this.listBox1.Items.Count; k++)
			{
				if (!list.Contains(this.listBox1.Items[k]))
				{
					FireHydantStruct fireHydantStruct2 = new FireHydantStruct();
					fireHydantStruct2.FireName = this.listBox1.Items[k].ToString();
					fireHydantStruct2.ComboBoxNozzleDiameter = this.xmlHelper.readMoRen("configuration").ComboBoxNozzleDiameter;
					fireHydantStruct2.ComboBoxFireDiameter = this.xmlHelper.readMoRen("configuration").ComboBoxFireDiameter;
					fireHydantStruct2.ComboBoxFireMaterial = this.xmlHelper.readMoRen("configuration").ComboBoxFireMaterial;
					fireHydantStruct2.ComboBoxFireLength = this.xmlHelper.readMoRen("configuration").ComboBoxFireLength;
					fireHydantStruct2.ComboBoxNozzleAngle = this.xmlHelper.readMoRen("configuration").ComboBoxNozzleAngle;
					fireHydantStruct2.CheckBox = this.xmlHelper.readMoRen("configuration").CheckBox;
					fireHydantStruct2.TextBoxWaterLevels = this.xmlHelper.readMoRen("configuration").TextBoxWaterLevels;
					this.listMoRen.Add(fireHydantStruct2);
				}
			}
			this.listBox1.SelectedItem = this.listBox1.Items[0];
			this.lastFireName = this.listBox1.SelectedItem.ToString();
		}

		private void SaveUserText()
		{
			foreach (FireHydantStruct fireHydantStruct in this.listMoRen)
			{
				if (fireHydantStruct.FireName == this.listBox1.SelectedItem.ToString())
				{
					fireHydantStruct.ComboBoxNozzleDiameter = this.comboBoxNozzleDiameter.SelectedItem.ToString();
					fireHydantStruct.ComboBoxFireDiameter = this.comboBoxFireDiameter.SelectedItem.ToString();
					fireHydantStruct.ComboBoxFireMaterial = this.comboBoxFireMaterial.SelectedItem.ToString();
					fireHydantStruct.ComboBoxFireLength = this.comboBoxFireLength.Text;
					fireHydantStruct.ComboBoxNozzleAngle = this.comboBoxNozzleAngle.Text;
					fireHydantStruct.CheckBox = this.helper.judgeTrueOrFalese(this.checkBox1.Checked);
					fireHydantStruct.TextBoxWaterLevels = this.textBoxWaterLevels.Text;
				}
			}
			this.xmlHelper.writeXML(this.listMoRen, "configuration");
		}

		private void getDrawCertal()
		{
			foreach (FireHydantStruct fireHydantStruct in this.listMoRen)
			{
				for (int i = 0; i < this.listResult.Count; i++)
				{
					if (this.listResult[i].Name == fireHydantStruct.FireName && fireHydantStruct.CheckBox == "0")
					{
						this.listResult.RemoveAt(i);
						i--;
					}
				}
			}
			foreach (Element element in this.listElement)
			{
				foreach (CalculationResult calculationResult in this.listResult)
				{
					if ((element as FamilyInstance).Symbol.Family.Name + " " + element.Name == calculationResult.Name)
					{
						DrawCercal drawCercal = new DrawCercal();
						drawCercal.fi = (element as FamilyInstance);
						drawCercal.r = double.Parse(calculationResult.R);
						this.listDraw.Add(drawCercal);
					}
				}
			}
		}

		private void buttonOK_Click(object sender, EventArgs e)
		{
			try
			{
				this.SaveUserText();
				this.listResult.Clear();
				this.listDraw.Clear();
				foreach (FireHydantStruct ss in this.listMoRen)
				{
					CalculationResult item = new CalculationResult();
					item = this.calculation(ss);
					this.listResult.Add(item);
				}
				foreach (CalculationResult calculationResult in this.listResult)
				{
					if (calculationResult.Name == this.listBox1.SelectedItem.ToString())
					{
						this.textBoxColumnLength.Text = calculationResult.Column;
						this.textBoxFireRadius.Text = calculationResult.R;
					}
				}
				this.buttonTXT.Enabled = true;
				this.isCal = true;
				this.getDrawCertal();
				if (this.listDraw.Count > 0)
				{
					this.button1.Enabled = true;
				}
				if (this.listDraw.Count == 0)
				{
					this.button1.Enabled = false;
				}
			}
			catch
			{
				MessageBox.Show("请确保输入的数字不为空，并且为数字!");
			}
		}

		private void buttonFind_Click(object sender, EventArgs e)
		{
			if (this.lineOperate.DeleteRangLine(this.doc, "FireHydrantCal", true))
			{
				this.doc.Regenerate();
				base.Close();
				this.listMoRen.Clear();
				this.listResult.Clear();
				this.listDraw.Clear();
				this.listElement.Clear();
				return;
			}
			MessageBox.Show("该视图没有您所要\n删除的范围检查线！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
		}

		private CalculationResult calculation(FireHydantStruct ss)
		{
			CalculationResult calculationResult = new CalculationResult();
			double qxh = double.Parse(ss.TextBoxWaterLevels);
			double b = this.helper.getB(ss.ComboBoxNozzleDiameter);
			double num = this.helper.gethq(qxh, b);
			double f = this.helper.getf(ss.ComboBoxNozzleDiameter);
			double substantialWaterColumn = this.helper.getSubstantialWaterColumn(num * 10.0, f);
			calculationResult.Column = Math.Round(substantialWaterColumn, 3).ToString();
			double a = double.Parse(ss.ComboBoxNozzleAngle) * Math.PI / 180.0;
			double fireR = this.helper.getFireR(double.Parse(ss.ComboBoxFireLength), substantialWaterColumn, a);
			calculationResult.R = Math.Round(fireR, 3).ToString();
			calculationResult.Name = ss.FireName;
			return calculationResult;
		}

		private void buttonESC_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void buttonTXT_Click(object sender, EventArgs e)
		{
		}

		private void SaveFileDialog()
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.Filter = "文本文件|*.txt";
			saveFileDialog.FilterIndex = 1;
			saveFileDialog.RestoreDirectory = true;
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				string text = saveFileDialog.FileName.ToString();
				string str = text.Substring(text.LastIndexOf("\\") + 1);
				text.Substring(0, text.LastIndexOf("\\"));
                //DateTime.Now.ToString("yyyyMMdd") + str;
				saveFileDialog.FileName.Insert(1, "dameng");
			}
		}

		private Element accordNameLookElem(string name)
		{
			Element result = null;
			foreach (Element element in this.listElement)
			{
				if ((element as FamilyInstance).Symbol.Family.Name + " " + element.Name == name)
				{
					result = element;
					break;
				}
			}
			return result;
		}

		private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			foreach (FireHydantStruct fireHydantStruct in this.listMoRen)
			{
				if (fireHydantStruct.FireName == this.lastFireName)
				{
					fireHydantStruct.ComboBoxNozzleDiameter = this.comboBoxNozzleDiameter.SelectedItem.ToString();
					fireHydantStruct.ComboBoxFireDiameter = this.comboBoxFireDiameter.SelectedItem.ToString();
					fireHydantStruct.ComboBoxFireMaterial = this.comboBoxFireMaterial.SelectedItem.ToString();
					fireHydantStruct.ComboBoxFireLength = this.comboBoxFireLength.Text;
					fireHydantStruct.ComboBoxNozzleAngle = this.comboBoxNozzleAngle.Text;
					fireHydantStruct.CheckBox = this.helper.judgeTrueOrFalese(this.checkBox1.Checked);
					fireHydantStruct.TextBoxWaterLevels = this.textBoxWaterLevels.Text;
				}
			}
			foreach (FireHydantStruct fireHydantStruct2 in this.listMoRen)
			{
				if (fireHydantStruct2.FireName == this.listBox1.SelectedItem.ToString())
				{
					this.comboBoxNozzleDiameter.SelectedItem = fireHydantStruct2.ComboBoxNozzleDiameter;
					this.comboBoxFireDiameter.SelectedItem = fireHydantStruct2.ComboBoxFireDiameter;
					this.comboBoxFireMaterial.SelectedItem = fireHydantStruct2.ComboBoxFireMaterial;
					this.comboBoxFireLength.Text = fireHydantStruct2.ComboBoxFireLength;
					this.comboBoxNozzleAngle.Text = fireHydantStruct2.ComboBoxNozzleAngle;
					this.checkBox1.Checked = this.helper.judgeTrueOrFalese(fireHydantStruct2.CheckBox);
					this.textBoxWaterLevels.Text = fireHydantStruct2.TextBoxWaterLevels;
				}
			}
			if (this.isCal)
			{
				foreach (CalculationResult calculationResult in this.listResult)
				{
					if (calculationResult.Name == this.listBox1.SelectedItem.ToString())
					{
						this.textBoxColumnLength.Text = calculationResult.Column;
						this.textBoxFireRadius.Text = calculationResult.R;
					}
				}
			}
			this.lastFireName = this.listBox1.SelectedItem.ToString();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			try
			{
				int num = 1;
				foreach (DrawCercal drawCercal in this.listDraw)
				{
					this.lineOperate.DrawRangLine(drawCercal.fi, drawCercal.r, "FireHydrantCal", num);
					num++;
				}
				base.Close();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
			this.listMoRen.Clear();
			this.listResult.Clear();
			this.listDraw.Clear();
			this.listElement.Clear();
		}

		private FireHydrantClaHelper helper = new FireHydrantClaHelper();

		public static string path = Path.Combine(Product.DataLocation, "User\\FrmCalFireHydant.xml");

		private FireHydantCalUserInfoReadandWrite xmlHelper = new FireHydantCalUserInfoReadandWrite(FrmCalFireHydant.path);

		private IList<Element> listElement = new List<Element>();

		private List<CalculationResult> listResult = new List<CalculationResult>();

		private List<FireHydantStruct> listMoRen = new List<FireHydantStruct>();

		private List<DrawCercal> listDraw = new List<DrawCercal>();

		private string lastFireName = "";

		private Document doc;

		private bool isCal;

		private AuxiliaryLineOperate lineOperate = new AuxiliaryLineOperate();
	}
}
